package com.gxuwz.medical.domain.chronicCard;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;

import com.gxuwz.medical.database.DBUtil;

/**
 * 慢性病证管理类
 * @author 沉羽墨玖
 *
 */
public class ChronicCard {
	private String id;           //慢性病证号
	private String persCode;    //农合证号/个人编号
	private String persName;    //姓名
	private String cardId;      //身份证号
	private String illName;     //疾病名称
	private String startTime;   //起始时间
	private String endTime;     //终止时间
	
	public ChronicCard(){
		
	}
	public ChronicCard(String id)throws SQLException{
		this.id=id;
		this.load();
	}
	public ChronicCard(String id, String persCode, String persName,
			String cardId, String illName, String startTime, String endTime) {
		super();
		this.id = id;
		this.persCode = persCode;
		this.persName = persName;
		this.cardId = cardId;
		this.illName = illName;
		this.startTime = startTime;
		this.endTime = endTime;
	}
	private void load()throws SQLException{
		Connection conn=null;
		 PreparedStatement pstmt=null;
		 ResultSet rs=null;
		try{
			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement("select * from chronicCard where id=?");
			int index = 1;
			pstmt.setString(index, this.id);
			rs=pstmt.executeQuery();
			rs.next();
			 this.id=rs.getString("id");
			 this.persCode =rs.getString("persCode");
			 this.persName =rs.getString("persName");
			 this.cardId =rs.getString("cardId");
			 this.illName =rs.getString("illName");
			 this.startTime=rs.getString("startTime");
			 this.endTime=rs.getString("endTime");
		 
		}catch(SQLException e){
			e.printStackTrace();
			throw e;
		}
		
	}
	/**
	 * 添加慢性病证
	 * @throws SQLException
	 */
	public void add()throws SQLException{
		Connection conn=null;
       try{
    	    conn=DBUtil.getConn();
    	    conn.setAutoCommit(false);
    	   //2；调用saveToDB方法将新添加的数据保存到数据�?
    	    saveToDB(conn);
    	    conn.commit();
       }catch(SQLException e){
    	   throw e;
       }finally{
    	   DBUtil.close(conn);
       }
		
	}
	/**
	 * 将新添加的数据保存到数据库
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			StringBuffer sqlBuff=new StringBuffer();
			sqlBuff.append("insert into chronicCard(id,persCode,persName,cardId,illName,startTime,endTime)");
			sqlBuff.append("values(?,?,?,?,?,?,?)");
			String sql=sqlBuff.toString();
			pstmt=conn.prepareStatement(sql);
			int index=1;
			 pstmt.setString(index++, this.id);
			 pstmt.setString(index++, this.persCode);
			 pstmt.setString(index++, this.persName);
			 pstmt.setString(index++, this.cardId);
			 pstmt.setString(index++, this.illName);
			 pstmt.setString(index++, this.startTime);
			 pstmt.setString(index, this.endTime);
			 pstmt.executeUpdate(); 
			
		}catch(SQLException e){
			 e.printStackTrace();
			throw e;
		}finally{
			DBUtil.close(pstmt);
			
		}
		
		
	}
	/**
	 * 删除慢性病证
	 * @param id
	 * @throws SQLException
	 */
	public void del(String id)throws SQLException{
		Connection conn=null;
		this.id=id;
       try{
    	    conn=DBUtil.getConn();
			conn.setAutoCommit(false);
    	   //调用delFromDB方法
    	    deleteFromDB(conn);
    	  //提交事务
			conn.commit();
       }catch(SQLException e){
    	   conn.rollback();
    	   throw e;
       }finally{
    	   DBUtil.close(conn);
       }
		
	}
	/**
	 * 删除慢性病证
	 * @param conn
	 * @throws SQLException
	 */
	private void deleteFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			StringBuffer sqlBuff=new StringBuffer("delete from chronicCard where id=?");
			pstmt=conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.id);
			pstmt.executeUpdate(); 
			
		}catch(SQLException e){
			throw new SQLException("Failed to delete record from table !", e);
		}finally{
			DBUtil.close(pstmt);
			
		}
		
		
	}
	
	/**
	 * 修改慢性病证
	 * @throws SQLException
	 */
	public void edit()throws SQLException{
		Connection conn=null;
		try{
			 conn =DBUtil.getConn();
			 conn.setAutoCommit(false);
			//更新慢性病证信息到数据库
			 editToDB(conn);
			 conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DBUtil.close(conn);
		}
	}
	/**
	 * 更新慢性病证
	 * @param conn
	 * @throws SQLException
	 */
	private void editToDB(Connection conn)throws SQLException{
		PreparedStatement pstmt=null;
		try{
			  StringBuffer sqlBuff=new StringBuffer("update chronicCard set persCode=?,persName=?,cardId=?,illName=?,startTime=?,endTime=? where id=?");
			  pstmt=conn.prepareStatement(sqlBuff.toString());
			//设置动态参数对应的值
			 int index=1;
			 pstmt.setString(index++, this.persCode);
			 pstmt.setString(index++, this.persName);
			 pstmt.setString(index++, this.cardId);
			 pstmt.setString(index++, this.illName);
			 pstmt.setString(index++, this.startTime);
			 pstmt.setString(index++, this.endTime);
			 pstmt.setString(index, this.id);
			 pstmt.executeUpdate(); 
			
		}catch(SQLException e){
			throw e;
		}finally{
			DBUtil.close(pstmt);
		}
		
	}
	
	

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	public String getPersCode() {
		return persCode;
	}
	public void setPersCode(String persCode) {
		this.persCode = persCode;
	}
	public String getCardId() {
		return cardId;
	}
	public void setCardId(String cardId) {
		this.cardId = cardId;
	}
	public String getIllName() {
		return illName;
	}
	public void setIllName(String illName) {
		this.illName = illName;
	}
	public void setPersName(String persName) {
		this.persName = persName;
	}
	public String getPersName() {
		return persName;
	}
	public String getStartTime() {
		return startTime;
	}
	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}
	public String getEndTime() {
		return endTime;
	}
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	
}


